//
//  BindUserViewController.m
//  PlayBall
//
//  Created by Huawen Liu on 12-8-14.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JddBindUserViewController.h"
#import "JddViewControllerManager.h"
#import "JddCommonFunction.h"
@interface JddBindUserViewController ()

@end

@implementation JddBindUserViewController

@synthesize realNameTextField;
@synthesize IDCardTextField;
@synthesize bgScrollView;
@synthesize modiBtn;

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddBindUserViewController);


-(BOOL)textFieldShouldClear:(UITextField *)textField{
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    return YES;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
    tapGestureRecognizer.delegate = self;
    [self.bgScrollView addGestureRecognizer:tapGestureRecognizer];
    
    int t =(self.view.frame.size.height - (modiBtn.frame.size.height+modiBtn.frame.origin.y));
    if(t>213){
        t = 0;
    }
    
    [self.bgScrollView setContentOffset:CGPointMake(0,t/*buyButton.frame.size.height + buyButton.frame.origin.y - mutipleTextField.frame.origin.y + mutipleTextField.frame.size.height + 20*/) animated:YES];
    return YES;
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:modiBtn]) {
        return NO;
    }
    
    return YES;
}

- (void)UpdateUserBaseInfoOperationResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    
    if (result) {
        if ([JddDataParser parseUserInfoResult:result]) {
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
//            [[JddViewControllerManager sharedInstance].navigationController popToViewController:[JddProfileUpdateViewController sharedInstance] animated:YES];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
        }
    }
}

- (IBAction)handleSingleTapGesture:(UITapGestureRecognizer*)sender{
    
    [realNameTextField resignFirstResponder];
    [IDCardTextField resignFirstResponder];
    [self.bgScrollView removeGestureRecognizer:sender];
    [self.bgScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    
}

- (IBAction)nextButtonClicked:(id)sender{
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^([0-9]{17})([0,1,2,3,4,5,6,7,8,9,x,X])$"];
    NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^([0-9]{15})$"];
    if (![IDCardTextField.text length]) {
        [JddCommonFunction messageBoxWithMessage:@"请输入正确的身份证号！"];
        return;
    } else {
        if ([IDCardTextField.text length] == 18){
            if (![pred evaluateWithObject:IDCardTextField.text]) {
                [JddCommonFunction messageBoxWithMessage:@"请输入正确的身份证号！"];
                return;
            }
        } else if ([IDCardTextField.text length] == 15) {
            if (![pred2 evaluateWithObject:IDCardTextField.text]) {
                [JddCommonFunction messageBoxWithMessage:@"请输入正确的身份证号！"];
                return;
            }
        } else {
            [JddCommonFunction messageBoxWithMessage:@"请输入正确的身份证号！"];
            return;
        }
    }
    if (![realNameTextField.text length] || ![IDCardTextField.text length]) {
            [JddCommonFunction messageBoxWithMessage:INFORMATION_NOT_INTACT];
            return;
    }
        
    [JddCommonFunction setWaitingViewHidden:NO fromController:self];
        
    JddUpdateUserBaseInfoOperation *operation = [[JddUpdateUserBaseInfoOperation alloc] init];
    operation.delegate = self;
    operation.realName =realNameTextField.text;
    operation.idcard = IDCardTextField.text;
    operation.mobile = @"";
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
}
    

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
   // [self.navigationItem setTitle:@"修改个人信息"];
    
        UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
        customLab.backgroundColor = [UIColor clearColor];
        [customLab setText:@"修改个人信息"];
        customLab.textAlignment = NSTextAlignmentCenter;

        [customLab setTextColor:[UIColor whiteColor]];
        customLab.font = [UIFont systemFontOfSize:19];
        self.navigationItem.titleView = customLab;
        
//        //self.navigationItem.hidesBackButton = YES;
    isNeedUpdate = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"bindUserInfo"];
    [MobClick event:@"UserCenter-binduserinfo"];
    realNameTextField.text = @"";
    IDCardTextField.text = @"";
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"bindUserInfo"];

    [realNameTextField resignFirstResponder];
    [IDCardTextField resignFirstResponder];
    //真实姓名的label和jdd_srk只显示一个
    [self setRealName];
}

-(void)setRealName
{
    if ([JddCommonFunction isAliPayUser]) {
        realNameTextField.text = [JddUserProfile sharedInstance].realName;
        realNameTextField.enabled = NO;
    }else{
        realNameTextField.enabled = YES;
    }
}

- (void)viewDidUnload
{
    [self setModiBtn:nil];
    [self setBgScrollView:nil];
    [super viewDidUnload];
}

@end
